﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using DCT.Model;
using System.Xml.Linq;

namespace DCT.ViewModel
{
    public class DeviceMapViewModelRepository
    {
        #region Constructor

        public DeviceMapViewModelRepository()
        {
            _deviceMapViewModels = new ObservableCollection<DeviceMapViewModel>();
        }

        #endregion // Constructor

        #region Fields

        ObservableCollection<DeviceMapViewModel> _deviceMapViewModels;

        #endregion // Fields

        #region Properties

        public ObservableCollection<DeviceMapViewModel> DeviceMapViewModels
        {
            get
            {
                return _deviceMapViewModels;
            }
        }

        public DeviceMapViewModel MainDeviceMapViewModel
        {
            get
            {
                return _deviceMapViewModels.FirstOrDefault(v => v.IsMainMap);
            }
        }

        #endregion // Properties

        #region Methods

        public void New()
        {
            this.Clear();

            DataAccess.Instance.CurrentSQLFileName = "New Project";
            DataAccess.Instance.CurrentXMLFileName = "New Project";

            this.Add(new DeviceMapViewModel(Resource.MainDeviceMapName, this));
        }

        public void Add(DeviceMapViewModel deviceMapViewModel)
        {
            _deviceMapViewModels.Add(deviceMapViewModel);

            WorkspaceManager.Instance.ActiveWorkspace = deviceMapViewModel;
        }

        public void Remove(DeviceMapViewModel deviceMapViewModel)
        {
            deviceMapViewModel.DeviceRepository.Clear();

            _deviceMapViewModels.Remove(deviceMapViewModel);

            WorkspaceManager.Instance.Close(deviceMapViewModel);
        }

        public bool Contains(DeviceMapViewModel deviceMapViewModel)
        {
            return _deviceMapViewModels.Contains(deviceMapViewModel);
        }

        public bool LoadProjectFile(string projectFile)
        {
            this.Clear();
            
            bool loadResult = false;

            string extensionString = System.IO.Path.GetExtension(projectFile);
            switch (extensionString.ToUpper())
            {
                case ".XML":
                    loadResult = DataAccess.Instance.LoadProjectFromXML(projectFile);
                    break;
                case ".DB":
                    loadResult = DataAccess.Instance.LoadProjectFromSQL(projectFile);
                    break;
                default:
                    break;
            }
            
            if (loadResult)
            {
                return ShowProject();
            }
            
            return false;
        }

        public bool SaveProjectFile(string projectFile)
        {
            bool saveResult = false;

            string extensionString = System.IO.Path.GetExtension(projectFile);
            switch (extensionString.ToUpper())
            {
                case ".XML":
                    saveResult = DataAccess.Instance.SaveProjectToXML(projectFile);
                    break;
                case ".S3DB":
                    saveResult = DataAccess.Instance.SaveProjectToSQL(projectFile);
                    break;
                default:
                    break;
            }

            return saveResult;
        }

        public bool ShowProject()
        {
            List<string> deviceMapViewNames = DataAccess.Instance.GetDeviceMapViewNames();

            if (deviceMapViewNames.Contains(Resource.MainDeviceMapName))
            {
                foreach (string deviceMapViewName in deviceMapViewNames)
                {
                    DeviceMapViewModel deviceMapViewModel = new DeviceMapViewModel(deviceMapViewName, this);

                    _deviceMapViewModels.Add(deviceMapViewModel);
                }

                //默认显示主视图
                WorkspaceManager.Instance.ActiveWorkspace = this.MainDeviceMapViewModel;

                return true;
            }

            return false;
        }

        public void Clear()
        {
            foreach (DeviceMapViewModel deviceMapViewModel in _deviceMapViewModels)
            {
                WorkspaceManager.Instance.Close(deviceMapViewModel);
                deviceMapViewModel.DeviceRepository.Clear();
            }

            _deviceMapViewModels.Clear();
            DataAccess.Instance.Clear();
        }

        public XElement GetXMLSerializedData()
        {
            XElement root = new XElement("Root");

            foreach (DeviceMapViewModel deviceMapViewModel in _deviceMapViewModels)
            {
                root.Add(deviceMapViewModel.DeviceRepository.GetXMLSerializedData());
            }

            return root;
        }

        public void ApplyChanges(XElement xmlChanges)
        {
            foreach (var xel in xmlChanges.Elements())
            {
                DeviceMapViewModel deviceMapViewModel = _deviceMapViewModels.FirstOrDefault(v => v.DisplayName == xel.Name.LocalName);

                if (deviceMapViewModel != null)
                {
                    deviceMapViewModel.DeviceRepository.ApplyChanges(xel);
                }
            }
        }

        #endregion // Methods
    }
}
